<?php
namespace app\admin\controller;

use think\Controller;
use think\Request;

class CommonController extends Controller
{
    //验证是否登陆的公共方法
    public function _initialize()
    {
        $user = \think\Session::get('uid');
        if( empty($user) ){
            $this->redirect('admin/login/index');
        }
    }

    /**
     * 排序的公共方法
     */
    public function change_order()
    {
        $request = Request::instance();//$test = $request->getInput(); //input组成的字符串
        $post_data = $request->post(); //获取所有的post数据

        $type = $post_data['type']; // category
        $field = $post_data['field'];
        $arr = array('category','article', 'friend', 'goods', 'user', 'config'); //定义允许的操作
        $data = array('msg' => '排序更新失败，请稍后重试！', 'status' => 1); //提示信息
        if ( ! in_array($type, $arr)) exit(json_encode($data));

        $class = '\app\admin\model\\'.ucfirst($type);

        $model = new $class;
        $new['id'] = $post_data['id'];
        $new[$field] = $post_data['order'];

        //1.注意：save方法只适合查询出数据，赋值修改$model->isUpdate()->save();
        if ($rs = $model->update($new) !== false) {
            $data = array('msg' => '排序更新成功！', 'status' => 0);
        }
        exit(json_encode($data));
    }

    /**
     * 上传图片公共类
     */
    public function upload()
    {
        $data = array('msg' => '上传失败', 'errno' => 1, 'path' => ''); //上传为空
        //17年7月26日新增 裁剪图片的操作
        if ( ($width = input('get.width')) && ($height = input('get.height')) )
        {
            if (isset($_FILES['file']) && $_FILES['file']['error'] == 0)
            {
                $file_type = $_FILES['file']['type'];
                if ( ( ($file_type == 'image/png') || ($file_type == "image/gif") || ($file_type == "image/jpeg") || ($file_type == "image/pjpeg"))
                    && ($_FILES["file"]["size"] < 1024*10240) ) {
                    //验证图片类型，图片大小，处理图片=》生成缩略图
                    $image = \think\Image::open($_FILES['file']['tmp_name']);
                    $path = 'uploads/public/thumb/'.order_sn().'.'.getExt($_FILES['file']['name']);
                    if ( !is_dir(dirname(ROOT_PATH . $path)) )
                    {
                        try{
                            mkdir(dirname(ROOT_PATH . $path), 0777, true);
                        }catch (\Exception $exception) {
                            $data['msg'] = '请添加 /uploads/ 目录写入权限, '.$exception->getMessage();
                            exit(json_encode($data));
                        }
                    }
                    $image->thumb($width,$height)->save(ROOT_PATH . $path);
                    $data = array('msg' => '图片上传成功', 'errno' => 0, 'path' => '/' . $path);
                }
            }
        } else {
            //$type = isset($_GET['type']) ? trim($_GET['type']) : 'static';
            // 获取表单上传文件 例如上传了001.jpg
            $file = request()->file('file');
            if ($file) {
                $type = request()->route('type');
                $type = isset($type) ? trim($type) : 'static';
                // 移动到框架应用根目录/uploads/public/$type/ 目录下
                $upload_path = 'uploads/public/' . $type . '/';

                //正常上传图片的处理
                $info = $file->move(ROOT_PATH . $upload_path);
                if ($info) {
                    // 成功上传后 获取上传信息
                    //echo $info->getExtension();   输出 jpg
                    //echo $info->getSaveName();    输出 20160820/42a79759f284b767dfcb2a0197904287.jpg
                    //echo $info->getFilename();    输出 42a79759f284b767dfcb2a0197904287.jpg
                    $name = str_replace("\\", '/', $info->getSaveName());
                    $data = array('msg' => '图片上传成功', 'errno' => 0, 'path' => '/' . $upload_path . $name);
                } else {
                    // 上传失败获取错误信息 echo $file->getError();
                    $data = array('msg' => $file->getError(), 'errno' => 2, 'path' => '');
                }
            }
        }
        //return $data;
        header("Content-Type:application/json;charset=utf-8");
        exit(json_encode($data));
    }

    /**
     * 公共操作 block表的查询和修改操作
     * @param string $type
     * @return mixed
     */
    public function common_block($type = '')
    {
        if (empty($type)) return '';
        if (request()->isPost()) {
            $data = input('post.');
            $str = serialize($data);
            //dump($str);
            return \think\Db::name('block')->where('type', $type)->update(['content' => $str]);
        }
        //获取首页区块内容
        $content = \think\Db::name('block')->field('content')->where('type', $type)->find();
        //反序列化 数据
        return unserialize($content['content']);
    }
}
